﻿using System;
using System.Linq;
using System.Security.Cryptography;

namespace King.Data
{
    /// <summary>
    /// 初始化数据
    /// </summary>
    public class DataInitializer
    {
        public void Create(DataBase context)
        {
            var system_user = context.Admin.FirstOrDefault(p => p.UserName.Equals("admin"));
            if (system_user == null)
            {
                //添加管理员  
                var admin = new Admin
                {
                    UserName = "admin",
                    RealName = "管理员",
                    Password = ToMD5("123qwe"),
                    CreationTime = DateTime.Now,
                    IsAdmin = true,
                    IsActive = true,
                    Roles = "0"
                };
                context.Admin.Add(admin);
                context.SaveChanges();
            }
        }

        private string ToMD5(string str)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] bytes_out = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(str));
            string result = BitConverter.ToString(bytes_out).Replace("-", "");
            return result;
        }
    }
}
